package com.ruicar.afs.cloud.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.entity.ProductDiscountSource;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.service.ProductDiscountSourceService;
import com.ruicar.afs.cloud.product.util.Vector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xudongdong
 * @date 2020-05-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/productDiscountSource")
@Api(value = "productDiscountSource", description = "产品贴息来源")
public class ProductDiscountSourceController {

    private final ProductDiscountSourceService productDiscountSourceService;

    @PostMapping(value = "/getSourceRuleInfoList")
    @ApiOperation(value = "多条件分页获取贴息规则方案")
    public IResponse<IPage<ProductDiscountSource>> listProductDiscountSouirce(@RequestBody QueryCondition<ProductDiscountSource> source) {
        return IResponse.success(productDiscountSourceService.page(new Page(source.getPageNumber(), source.getPageSize()), Wrappers.<ProductDiscountSource>query().lambda()
                .like(StringUtils.isNotEmpty(source.getCondition().getDiscountSourceName()), ProductDiscountSource::getDiscountSourceName, source.getCondition().getDiscountSourceName())
                .eq(StringUtils.isNotEmpty(source.getCondition().getStatus()), ProductDiscountSource::getStatus, source.getCondition().getStatus())
        ));
    }

    @RequestMapping(value = "/saveSource", method = RequestMethod.POST)
    @ApiOperation(value = "贴息来源保存")
    public IResponse<Boolean> saveFinancePlan(@RequestBody ProductDiscountSource productDiscountSource) {
        String str = productDiscountSource.getRuleExpression();
        String p1 = "#\\{[^\\{\\}]{1,}\\}";//没有确定回{后面一定是答(
        Pattern p = Pattern.compile(p1);
        Matcher m = p.matcher(str);
        while (m.find()) {
            str = str.replace(m.group() + "", "10");
        }
        try {
            Double dou = Vector.computeWithVector(str);
        } catch (Exception e) {
            return IResponse.fail("规则填写不正确");
        }
        if (productDiscountSource.getId() == null) {
            productDiscountSource.setStatus("0");
            productDiscountSourceService.save(productDiscountSource);
        } else {
            productDiscountSourceService.updateById(productDiscountSource);
        }
        return IResponse.success(productDiscountSource);
    }

    @Transactional
    @PostMapping(value = "/effectiveDiscountPlan/{ids}")
    @ApiOperation(value = "贴息规则方案生效操作")
    public IResponse<Boolean> effectiveByIds(@PathVariable List<Long> ids) {
        for (Long id : ids) {
            ProductDiscountSource source = productDiscountSourceService.getById(id);
            source.setStatus(AfsEnumUtil.key(StatusEnum.YES));
            productDiscountSourceService.updateById(source);
        }
        return new IResponse<Boolean>().setMsg("操作成功");
    }

    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除贴息规则方案")
    public IResponse<Boolean> deletePlan(@PathVariable String id) {
        ProductDiscountSource productDiscountSource = productDiscountSourceService.getById(id);
        if (AfsEnumUtil.key(StatusEnum.YES).equals(productDiscountSource.getStatus())) {
            return new IResponse<Boolean>().setMsg("生效方案不可删除！");
        } else {
            productDiscountSourceService.removeById(id);
            return new IResponse<Boolean>().setMsg("删除数据成功!");
        }
    }
}
